package com.sm.controller;

import com.sm.model.SystemInformation;
import com.sm.service.MyTeamService;
import com.sm.service.SystemInformationService;
import com.sm.util.Msg;
import com.sm.util.PageBean;
import com.sm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统通知controller
 *
 * @author 60915
 */
@RequestMapping("/systemInformation")
@Controller
public class SystemInformationController {

	@Autowired
	private SystemInformationService systemInformationService;

	/**
	 * 获取我的系统通知列表
	 *
	 * @param type          类型 -1 全部消息 1我的消息 2系统消息
	 * @return userId		用户ID
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Msg selectMySystemInformationList(@RequestParam Integer type,
											 @RequestParam Integer userId,
											 PageBean pageBean) {
		PageResult pageResult = systemInformationService.selectSysListByReceiver(pageBean.getCurrentPage(), pageBean.getPageSize(),
				userId, type);
		return Msg.success().add("msg", "列表拉取成功").add("data", pageResult);
	}

	/**
	 * 查询消息详情
	 *
	 * @return userId		用户ID
	 */
	@RequestMapping("/selectOne")
	@ResponseBody
	public Msg selectOne(@RequestParam Integer id) {
		SystemInformation systemInformation = systemInformationService.selectByPrimaryKey(id);
		if (systemInformation == null) {
			return Msg.fail().add("msg", "消息不存在");
		}
		systemInformation.setInfoStatus(0);
		systemInformationService.updateByPrimaryKeySelective(systemInformation);
		return Msg.success(systemInformation);
	}
	/**
	 * 删除消息
	 *
	 * @return userId		用户ID
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public Msg remove(@RequestParam List<Integer> ids) {
		if (ids == null || ids.size() == 0) {
			return Msg.fail().add("msg", "请选择消息");
		}
		systemInformationService.removeById(ids);
		return Msg.success();
	}


	/**
	 * 根据状态查询数量
	 * @param userId
	 * @param  type 类型 -1 全部消息 1我的消息 2系统消息
	 * @param  infoStatus //-1 全部 0是已读，1是未读
	 * @return
	 */
	@RequestMapping("/selectCount")
	@ResponseBody
	public Msg selectCountInfo(Integer userId,Integer type,Integer infoStatus) {
		int count = systemInformationService.selectSysInfoCount(userId, type, infoStatus);
		return Msg.success().add("count",count);
	}

}
